package com.han.hotelplat.comm.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import com.han.hotelplat.comm.Constant;

public class UploadFile {
	
	public static String uploadImg(MultipartFile file, String fileName, String saveDir) throws Exception {
		// 文件的访问地址
		String fileRemoteUrl = null;
		String sourcePath = "source";
		try {
			// 文件有效
			if (null != file && file.getBytes().length > 0 && checkImgType(fileName)) {
				// 截取文件的后缀名
				String suffix = fileName.substring(fileName.lastIndexOf("."));
				// 生成新的文件名
				Double random = Math.random() * 100000;
				String newFileName = System.currentTimeMillis() + "_" + random.intValue() + suffix;
				String path = Constant.IMG_PATH + File.separator
						+ saveDir + File.separator;
				//目录不存在要创建
				sourcePath = path + sourcePath + File.separator;
				FileUtils.createDir(sourcePath);
				// 文件的绝对路径
				File outFile = new File(sourcePath+newFileName);//缩略图目录
				file.transferTo(outFile); // 写入原图
				Snippet.createThumbnail(sourcePath+newFileName, path+newFileName, 360, 360);
				fileRemoteUrl = path.substring(path.lastIndexOf("uploadImg"))+newFileName;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return fileRemoteUrl;
	}
	
	public static List<String> uploadImgs(List<MultipartFile> files, String savePath, HttpServletRequest request, String fptPath) throws Exception {
		List<MultipartFile> filess = new ArrayList<MultipartFile>(files);
		List<String> localFilepath = new ArrayList<String>();// 文件的访问地址
		List<String> localFileName = new ArrayList<String>();
		String tempUrl = request.getRequestURL().toString();
		try {
			if(filess != null){
				for (int i = 0; i < filess.size(); i++) {
					String fileName = filess.get(i).getOriginalFilename();
					localFileName.add(fileName);
					// 文件有效
					if (null != filess.get(i) &&
							filess.get(i).getBytes().length > 0 && checkImgType(fileName)
							) {
						// 获取文件的绝对保存路径
						String realPath = request.getSession().getServletContext().getRealPath(savePath);
						System.out.println(String.format("realPath is %s", realPath));
						FileUtils.createDir(realPath);
						// 截取文件的后缀名
						String suffix = fileName.substring(fileName.lastIndexOf("."));
						// 生成新的文件名
						Double random = Math.random() * 100000;
						String newFileName = System.currentTimeMillis() + "_" + random.intValue() + suffix;
						// 文件的绝对路径
						File outFile = new File(realPath + File.separator + newFileName);
						filess.get(i).transferTo(outFile); // 写入
						FileUtils.saveFile(outFile, savePath, fileName, true);
						String fileRemoteUrl = tempUrl + File.separator+savePath+File.separator+newFileName;
						localFilepath.add(fileRemoteUrl);
					} else {
						filess.remove(i);
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return localFilepath;
	}

	public static void deleteFile(String fileRemoteUrl, String savePath, HttpServletRequest request) {
		try {
			if (null != fileRemoteUrl && !fileRemoteUrl.matches("\\s*")) {
				String fileName = fileRemoteUrl.substring(fileRemoteUrl.lastIndexOf("/") + 1);
				String realPaht = request.getSession().getServletContext().getRealPath(savePath);
				File file = new File(realPaht + File.separator + fileName);
				if (file.exists()) {
					file.delete();
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static String[] imgType = new String[] { ".bmp", ".ief", ".jpe", ".jpeg", ".jpg", ".pgm", ".png", ".pnm", ".ppm", ".ras", ".rgb", ".tif", ".tiff", ".xbm", ".xpm", ".xwd", ".ico", ".gif" };
	public static String[] audioType = new String[] { ".aif", ".aifc", ".aiff", ".au", ".mp2", ".mp3", ".snd", ".wav" };
	public static String[] videoType = new String[] { ".avi", ".m1v", ".mov", ".mpa", ".mpe", ".mpeg", ".mpg", ".qt" };

	public static boolean checkImgType(String fileName) {
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		for (String type : imgType) {
			if (suffix.equalsIgnoreCase(type)) {
				return true;
			}
		}
		return false;
	}

}
